package com.cjb.common.util;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.RemoteIterator;
import org.springframework.http.ResponseEntity;

import java.io.*;
import java.math.BigDecimal;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class WebUtil
 * @Date 2023-10-22 下午7:16
 */
public class WebUtil {
    public static void downLoad(FSDataInputStream file, HttpServletResponse response, String filePath) throws Exception {
        byte[] buffer = new byte[1024];
        int length;
        while ((length = file.read(buffer)) > 0) {
            response.setContentType("application/octet-stream");

            response.setHeader("Content-Disposition", "attachment; filename=" + filePath.substring(filePath.lastIndexOf("/") + 1));
            // Write the data to the response
            ServletOutputStream out = response.getOutputStream();
            out.write(buffer, 0, length);
        }
        // Close the file
        file.close();
    }
    public static void downloadByte(byte[] file, HttpServletResponse response, String filePath) throws Exception {
        InputStream in = new ByteArrayInputStream(file);
        try {
            OutputStream out = response.getOutputStream();

            // Set content type and headers
            response.setContentType("application/octet-stream");
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }

            out.flush();
        } finally {
            in.close();
        }
    }
    public static void preview(FSDataInputStream file, HttpServletResponse response, String filePath) throws Exception {
        byte[] buffer = new byte[1024];
        int length;
        while ((length = file.read(buffer)) > 0) {
            // 根据文件类型设置Content-Type
            String contentType = getContentType(filePath);
            response.setContentType(contentType);

            // 设置Content-Disposition为inline，以在浏览器中预览图片
            response.setHeader("Content-Disposition", "inline; filename=" + filePath.substring(filePath.lastIndexOf("/") + 1));

            // 写入数据到响应
            ServletOutputStream out = response.getOutputStream();
            out.write(buffer, 0, length);
        }
        // 关闭文件
        file.close();
    }
    public static void previewPDF(FSDataInputStream file, HttpServletResponse response, String filePath) throws Exception {
        byte[] buffer = new byte[1024];
        int length;
        while ((length = file.read(buffer)) > 0) {
            // Set the Content-Type to application/pdf
            response.setContentType("application/pdf");

            // Set Content-Disposition to inline to preview the PDF in the browser
            response.setHeader("Content-Disposition", "inline; filename=" + filePath.substring(filePath.lastIndexOf("/") + 1));

            // Write the data to the response
            ServletOutputStream out = response.getOutputStream();
            out.write(buffer, 0, length);
        }
        // Close the file
        file.close();
    }

    private static String getContentType(String filePath) {
        String contentType = "application/octet-stream";
        if (filePath.endsWith(".jpg") || filePath.endsWith(".jpeg")) {
            contentType = "image/jpeg";
        } else if (filePath.endsWith(".png")) {
            contentType = "image/png";
        }
        // 添加其他文件类型的处理...
        return contentType;
    }
}
